package com.sqllog.admin.auth.service;


import com.sqllog.admin.auth.domain.Menu;
import com.sqllog.admin.auth.mapper.MenuMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * 菜单服务
 *
 * @author chennengcheng
 * @date 2022-01-14 14:35
 * @remark
 */

@Service
public class MenuService {

    @Resource
    private MenuMapper menuMapper;

    /**
     * Describe: 获取用户菜单
     * Param: username
     * Return: Result
     */
    public List<Menu> getUserMenu(String userName) {
        if ("admin".equals(userName)) {
            return menuMapper.selectAll();
        }else {
            return menuMapper.selectAllEnable();
        }
    }

    /**
     * Describe: 递归获取菜单tree
     * Param: Menus
     * Return: 操作结果
     */
    public List<Menu> toUserMenu(List<Menu> Menus, String parentId) {
        List<Menu> list = new ArrayList<>();
        for (Menu menu : Menus) {
            if (parentId.equals(menu.getParentId())) {
                menu.setChildren(toUserMenu(Menus, menu.getId()));
                list.add(menu);
            }
        }
        return list;
    }
}
